using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Comments;
using Framework.Module.CMS.Library.Contents;
using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components.Commands;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Admin.WorkFlow
{
    public partial class CommentList : AuthenticatedControl
    {
        private int contentid;
        protected void Page_Load(object sender, EventArgs e)
        {
              
           
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            contentid = ConvertUtility.ToInt32(Request.QueryString["id"]);
            lblUpdateStatus.Text = string.Empty;
            ContentInfo cinfo = ContentController.GetContent(contentid);
            lnkHeadline.Text = cinfo.Content_Headline;
            lnkHeadline.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.CMS_ViewContent)) + "&contentID=" + contentid;
            int zoneid = cinfo.Content_ZoneID;
            Label1.Text = ZoneController.GetZoneNameByZoneID(zoneid);
            LoadData();
        }
        protected void LoadData()
        {
            DataGrid1.DataSource = CommentDB.GetByContentID(contentid, false);
            DataGrid1.DataBind();
        }
        protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                DataRowView curData = (DataRowView)e.Item.DataItem;

                //HyperLink lnkComment = (HyperLink)e.Item.FindControl("lnkComment");
                //lnkComment.Text = curData["Comment_Content"].ToString();
                //lnkComment.NavigateUrl = AppEnv.AdminUrlParams("cms_viewcontent") + "&contentid=" + curData["Content_ID"].ToString();

                //int userid = ConvertUtility.ToInt32(curData["User_ID"].ToString());
                //string username = "";

                //UserInfo info = UserController.GetUser(userid);
                //if (info != null)
                //    username = info.DisplayName;

                //string tooltip = MiscUtility.ToolTipForComment(username, ConvertUtility.ToString(curData["Comment_Name"]), ConvertUtility.ToString(curData["Comment_Email"]), ConvertUtility.ToString(curData["Content_Headline"]), ConvertUtility.ToString(curData["Content_SubTitle"]), ConvertUtility.ToString(curData["Content_Avatar"]), ConvertUtility.ToString(curData["Content_Teaser"]));
                //lnkComment.Attributes.Add("onmouseover", "showtip('" + tooltip + "','322','#fcf9ea')");
                //lnkComment.Attributes.Add("onmouseout", "hidetip()");

                Label lblDateTime = (Label)e.Item.FindControl("lblDateTime");
                lblDateTime.Text = ConvertUtility.ToDateTime(curData["Comment_Datetime"]).ToShortDateString();
            }
        }
        protected void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                CommentDB.Delete(Int32.Parse(e.Item.Cells[0].Text));
                LoadData();
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
        }

        protected void btnAcceptAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridItem item in DataGrid1.Items)
            {
                CheckBox chkCheck = (CheckBox)item.FindControl("chkCheck");
                int id = Int32.Parse(item.Cells[0].Text);
                CommentInfo info = CommentDB.GetInfo(id);
                if (info.User_ID == 0)
                    CommentDB.Accept(id, chkCheck.Checked, CurrentUser.UserID);
                else
                    CommentDB.Accept1(id, chkCheck.Checked);
            }
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
        }

        protected void btnReadAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridItem item in DataGrid1.Items)
            {
                CheckBox chkRead = (CheckBox)item.FindControl("chkRead");
                int id = Int32.Parse(item.Cells[0].Text);
                CommentDB.UpdateRead(id, chkRead.Checked);
            }
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
        }
    }
}